﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using eCommerce.DataAccess;

namespace eCommerce.CMS.CustomAdmin.Controls.Pages
{
    public partial class Edit : System.Web.UI.UserControl
    {
        Int32 _pageID;
        Xcms_Page _pag;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["PageID"] != null)
            {
                if (Int32.TryParse(Request.QueryString["PageID"], out _pageID))
                {
                }
                else
                {
                    Response.Redirect("~/");
                }
            }
            if (!Page.IsPostBack)
            {
                if (_pageID > 0)
                {
                    using (WebsiteDataContext adatabaseDataContext = new WebsiteDataContext())
                    {
                        try
                        {
                            _pag = (from x in adatabaseDataContext.Xcms_Pages
                                   where x.pageid == _pageID
                                   select x).Take(1).SingleOrDefault();
                            if (_pag != null)
                            {
                                tbxID.Text = _pag.pageid.ToString();
                                //tbxParent.Text = _pag.pageupid;
                                tbxMarker.Text = _pag.pagemarker;
                                tbxTitle.Text = _pag.pagetitle;
                                tbxNavi.Text = _pag.pagenavigation;
                                if (_pag.pagecontent != null)
                                {
                                    FCKText.Value = _pag.pagecontent.Trim();
                                }
                                else
                                {
                                    FCKText.Value = "no text";
                                }
                                tbxMetaKey.Text = _pag.pagemetatags;
                                tbxMetaDesc.Text = _pag.pagemetadescription;
                                tbxSpecial.Text = _pag.pagespecial;
                                tbxPosition.Text = _pag.pageorder.ToString();
                                tbxUseCont.Text = _pag.pageusecontent.ToString();
                                tbxSuppressNavi.Text = _pag.pagesuppressnavigation.ToString();
                                tbxSuppressHotT.Text = _pag.pagesuppresshottext.ToString();
                                tbxUseColl.Text = _pag.pageusecollection.ToString();
                                tbxSecured.Text = _pag.pagerestrict.ToString();
                                tbxNewWind.Text = _pag.pagenewwindow.ToString();
                                UpdateDateTbx.Text = DateTime.Now.ToShortDateString();
                            }
                        }
                        catch (Exception ex)
                        {
                            userformPanelStatus.Enabled = true;
                            userformPanelStatus.Visible = true;
                            userformPanelAdd.Enabled = false;
                            userformPanelAdd.Visible = false;
                            PanelStatusMsg.Text = "No user found. Message: " + ex.Message;
                        }
                    }
                }
                WebsiteDataContext db = new WebsiteDataContext();
                var query = db.getParent();
                ddlParent.DataSource = query;
                ddlParent.DataTextField = "pagetitle";
                ddlParent.DataValueField = "pageid";
                ddlParent.DataBind();
                ddlParent.Items.Insert(0, new ListItem("None", "0"));
            }
        }
        protected void EditBtn_Click(object sender, EventArgs e)
        {
            using (WebsiteDataContext ndc = new WebsiteDataContext())
            {
                var pag = (from p in ndc.Xcms_Pages where (p.pageid == Convert.ToInt32(this._pageID)) select p).Single();
                //pag.pageupid = Convert.ToInt32(tbxParent.Text);
                pag.pageupid = Convert.ToInt32(ddlParent.SelectedValue);
                pag.pagemarker = tbxMarker.Text;
                pag.pagetitle = tbxTitle.Text;
                pag.pagenavigation = tbxNavi.Text;
                pag.pagecontent = FCKText.Value;
                pag.pagemetatags = tbxMetaKey.Text;
                pag.pagemetadescription = tbxMetaDesc.Text;
                pag.pagespecial = tbxSpecial.Text;
                //pag.externallink = Convert.ToBoolean(tbxExternal.Text);
                pag.pageorder = Convert.ToInt32(tbxPosition.Text);
                pag.pageusecontent = Convert.ToBoolean(tbxUseCont.Text);
                pag.pagesuppressnavigation = Convert.ToBoolean(tbxSuppressNavi.Text);
                pag.pagesuppresshottext = Convert.ToBoolean(tbxSuppressHotT.Text);
                pag.pageusecollection = Convert.ToInt32(tbxUseColl.Text);
                pag.pagerestrict = Convert.ToBoolean(tbxSecured.Text);
                pag.pagenewwindow = Convert.ToBoolean(tbxNewWind.Text);
                //pag.updatedate = Convert.ToDateTime(UpdateDateTbx.Text);

                ndc.SubmitChanges();
                if (pag.pageid == 0)
                {
                    lblTest.Text = "Error";
                }
                else
                {
                    lblTest.Text = "Details have been updated";
                }
            }
        }
        protected void lnkbtnDev_Click(object sender, EventArgs e)
        {
            pnlDevelop.Visible = true;
        }

        protected void lnkbtnUnDev_Click(object sender, EventArgs e)
        {
            pnlDevelop.Visible = false;
        }
    }
}